<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Juez de sebas</title>
        <link href="resources/vividorange/style.css" rel="stylesheet" type="text/css" media="screen" />
    </head>
    <body onload="initPage()">
        <?php
        include('modelo.php');
        session_start();
        if (!isset($_SESSION['username']))
            header('Location: login.php');
        $userid = $_SESSION['username'];
        $username = executeScalar("select Nombre from Usuarios where cod='$userid'");

        function listValidExer() {
            $mysqldate = date( 'Y-m-d H:i:s', time() );
            $sql = "select idEjercicio,Nombre from Ejercicios where FechaIni < '$mysqldate' and '$mysqldate' < FechaFin";
            $ans = executeQuery($sql);
            for ($i=0; $i<count($ans); $i++) {
                $row = $ans[$i];
                $id = $row[0]; $name = $row[1];
                echo "<li>";
                echo "<a href='javascript:viewExercise($id)'>$name</a>";
                echo "</li>\n";
            }
        }
        ?>

        <div id="wrapper">

            <div id="menu">
                <ul>
                    <li><a href="logout.php">Salir</a></li>
                </ul>
            </div>
            <div id="header">
                <div id="logo">
                <h1>Estructura de datos</h1>
                Sebastián Gómez González - Universidad Tecnologica de Pereira
                </div>
            </div>
            <div id="page">
            <div id="page-bgtop">
            <div id="page-bgbtm">

                <div id="content">

                    <div class="post" id="mainForm">

                    </div>
                    
                    <div class="post" id="problemDesc">
                        
                    </div>

                    <div class="post" id="ayuda">

                    </div>
                </div>

                <div id="sidebar">
                    <ul>
                        <li>
                            <ul>
                                <h2>Ejercicios</h2>
                                <?php listValidExer() ?>                                
                            </ul>
                        </li>

                        <li id="problems">
                            <ul>
                                Seleccione un ejercicio
                            </ul>
                        </li>
                    </ul>
                </div>

            </div></div></div>
        </div>
                
        <script type="text/javascript" src="resources/jquery.js"></script>
        <script type="text/javascript" src="resources/jquery.form.js"></script>

        <script language="javascript">
            var timeLimit=<?php echo $timeLimit-60 ?>;
            var mytime, timerId, timerOn;
            var gifLoader = '<img src="resources/loader.gif" />';

            function initPage() {
                $.ajax({
                    url: 'resources/help.txt',
                    success: function (html) {
                        var m = document.getElementById("ayuda");
                        m.innerHTML = html;
                    }
                });
                timerOn = 0;
            }
            
            function viewExercise(id) {
                var myurl = 'ejercicio.php?id=' + id;
                $("#mainForm").empty().html(gifLoader);
                $.ajax({
                    url: myurl,
                    success: function(html) {
                        var atr = document.getElementById("problems");
                        atr.innerHTML = html;
                        $("#mainForm").empty();
                    }
                });                
            }

            function viewProblem(idEx, idProb) {
                var myurl = 'problem.php?idEj='+idEx+'&idProb='+idProb;
                $("#mainForm").empty().html(gifLoader);
                $.ajax({
                    url: myurl,
                    success: function(html) {
                        var atr = document.getElementById("problemDesc");
                        atr.innerHTML = html;
                        $("#mainForm").empty();
                    }
                });
            }

            function myTimer() {
                if (mytime > 0 && timerOn==1) {
                    var x = document.getElementById("timer");
                    mytime--;
                    var sec = mytime%60, min = (mytime-sec)/60;
                    var time = min + ':' + ((sec < 10) ? ('0' + sec) : sec);
                    x.innerHTML= 'Límite de tiempo: <h2>' + time + "</h2>";
                    t = setTimeout("myTimer()", 1000);
                } else {
                    var x = document.getElementById("mainForm");
                    clearTimeout(t);
                    x.innerHTML = "Se ha vencido el límite de tiempo";
                }
            }

            function beforeSubmission() {
                if ($('#srcfile').val() == '' || $('#outfile').val() == '') {
                    alert("Debe seleccionar un archivo de salida y el fuente");
                    return false;
                }
                clearTimeout(t);
                timerOn=0;
                //var x = document.getElementById("mainForm");
                //x.innerHTML = gifLoader;
                //return true;
            }
            
            function getInputFile(idEx, idProb) {
                var myurl = 'submissions.php?idEj='+idEx+'&idProb='+idProb;
                $.ajax({
                    url: myurl,
                    success: function(html) {
                        var atr = document.getElementById("mainForm");
                        atr.innerHTML = html;
                        mytime = timeLimit;
                        timerOn = 1;
                        myTimer();                        
                    }
                });
            }
            
        </script>

    </body>
</html>
